//
//  HBGetUserStatusManager.m
//  HuoBao
//
//  Created by chaiyanan on 15/12/3.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBGetUserStatusManager.h"
#import "HBGetUserStatusParam.h"
#import "HBGetUserStatusParser.h"

#define CurrentUserStatus     @"HuoBaoCurrentUserStatus"

@implementation HBGetUserStatusManager

static HBGetUserStatusManager *sharedHBGetUserStatusManager = nil;

+ (HBGetUserStatusManager *)sharedHBGetUserStatusManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedHBGetUserStatusManager = [[HBGetUserStatusManager alloc]init];
    });
    return sharedHBGetUserStatusManager;
}

+ (void)requestToGetUserStatusWithResult:(void (^)(BOOL, NSString *, NSError *))result
{
    HBGetUserStatusParam *param = [[HBGetUserStatusParam alloc]init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBGetUserStatusParser *parser = [[HBGetUserStatusParser alloc]init];
    [NetCenter getDataFromURL:Method_getUserStatus
                       params:param
                       parser:parser
                       result:^(ParserResult *myResult)
     {
         if (myResult.code ==0 )
         {
             HBGetUserStatusInfo *info = (HBGetUserStatusInfo *)myResult.result;
             
             [self cacheCurrentUserToUserDefault:info];
             if (result)
             {
                 result(YES, @"", nil);
             }
         }
         else
         {
             if (result)
             {
                 result(NO, myResult.message, nil);
             }
         }
         
     } failure:^(NSError *error)
     {
         if (result)
         {
             result(NO, @"", error);
         }
     }];
}

+ (void)cacheCurrentUserToUserDefault:(HBGetUserStatusInfo *)info
{
    NSData *userInfoData = [NSKeyedArchiver archivedDataWithRootObject:info];
    [[NSUserDefaults standardUserDefaults]setObject:userInfoData forKey:CurrentUserStatus];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self sharedHBGetUserStatusManager].userStatusInfo = info;
    if (info.identityStatus.length>0&&info.bindCardStatus.length>0)
    {
        if ([info.identityStatus integerValue]&&[info.bindCardStatus integerValue])
        {
            [self sharedHBGetUserStatusManager].isAllAuthor = YES;
        }
        else
        {
            [self sharedHBGetUserStatusManager].isAllAuthor = NO;
        }
    }
    else
    {
       [self sharedHBGetUserStatusManager].isAllAuthor = NO;
    }
    
    
}

+ (HBGetUserStatusInfo *) getCurrentUserStatusInfoFromUserdefault
{
    HBGetUserStatusInfo *info = [[HBGetUserStatusInfo alloc]init];
    info = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults]objectForKey:CurrentUserStatus]];
    if (info)
    {
        [self sharedHBGetUserStatusManager].userStatusInfo = info;
        if (info.identityStatus.length>0&&info.bindCardStatus.length>0)
        {
            if ([info.identityStatus integerValue]&&[info.bindCardStatus integerValue])
            {
                [self sharedHBGetUserStatusManager].isAllAuthor = YES;
            }
            else
            {
                [self sharedHBGetUserStatusManager].isAllAuthor = NO;
            }
        }
        else
        {
            [self sharedHBGetUserStatusManager].isAllAuthor = NO;
        }
        
    }
    return info;
}

+ (void) deleteCacheCurrentUserStatusInfoFromUserDefault
{
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:CurrentUserStatus];
    [[NSUserDefaults standardUserDefaults]synchronize];
    [self sharedHBGetUserStatusManager].userStatusInfo = nil;
    [self sharedHBGetUserStatusManager].isAllAuthor = NO;
}




@end
